import store from '@/store'

function checkPermission(el, bindding) {
  const { value } = bindding
  const roles = store.getters && store.getters.roles

  if (value && value instanceof Array) {
    if (value.length > 0) {
      const permissionRoles = value

      const hasPermission = roles.some(role => permissionRoles.includes(role) || role === 'admin')

      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    }
  } else {
    throw new Error(`需要格式为 ['admin','editor']`)
  }
}

export default {
  inserted(el, bindding) {
    checkPermission(el, bindding)
  },
  update(el, bindding) {
    checkPermission(el, bindding)
  }
}
